import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Scanner;

/**
 * @author yu
 * @date 2025年05月14日 18:48
 *///TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or
// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
public class Main {
    static int[][] ter;
    static int size;
    static int[] ddx={1,0};
    static int[] ddy={0,1};

    static class Pointt{
        int x,y,cost;
        Pointt(int x, int y, int cost){
            this.x = x;
            this.y = y;
            this.cost = cost;
        }
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        try {
            if(!sc.hasNextInt()){
                System.out.print("-2");
                return;
            }
            size = sc.nextInt();
            if(size <=0 || size>100){
                System.out.print("-2");
                return;
            }

            ter = new int[size][size];
            for(int ii=0;ii<size;ii++){
                for(int jj=0;jj<size;jj++){
                    if(!sc.hasNextInt()){
                        System.out.print("-2");
                        return;
                    }
                    ter[ii][jj] = sc.nextInt();
                    if(ter[ii][jj] < 0 || ter[ii][jj] > 10){
                        System.out.print("-2");
                        return;
                    }
                }
            }

            int res = comMinStaina();
            System.out.println(res);
        }catch (Exception e) {
            System.out.print("-2");
        }
    }

    private static int comMinStaina(){
        int[][] minStaina = new int[size][size];
        for(int[] row : minStaina){
            Arrays.fill(row, Integer.MAX_VALUE);
            minStaina[0][0] = ter[0][0];

            PriorityQueue<Pointt> pq = new PriorityQueue<>(Comparator.comparingInt(p -> p.cost));
            pq.offer(new Pointt(0, 0, ter[0][0]));

            while(!pq.isEmpty()){
                Pointt cur = pq.poll();
                int x = cur.x;
                int y = cur.y;
                int cost = cur.cost;



                for(int dir =0;dir<2;dir++){
                    int nx = x + ddx[dir];
                    int ny = y + ddy[dir];

                    if(nx >= 0 && nx < size && ny >=0 && ny < size){
                        int heightDi = Math.abs(ter[nx][ny] - ter[x][y]);
                        if(heightDi <= 1){
                            int newCost = cost + ter[nx][ny];
                            if(newCost < minStaina[nx][ny]){
                                minStaina[nx][ny] = newCost;
                                pq.offer(new Pointt(nx, ny, newCost));
                            }
                        }
                    }
                }
            }
        }
        int res = Integer.MAX_VALUE;
        for(int i=0;i<size;i++){
            res = Math.min(res, minStaina[i][size-1]);
        }
        return res == Integer.MAX_VALUE ? -1 : res;
    }
}